home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Interprogram Messaging Manager / IPM MessageBoard / myevents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-23  |  4.8 KB  |  286 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * IPM MessageBoard AOCE Sample
  4.  *
  5.  * ©1992-1993 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * events.c -- main event loop and basic event handling
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        2/12/93        1.0b1        udpate to AOCE beta seed
  14.  * SJF        11/6/91        1.0d1        initial coding
  15.  *
  16.  */
  17.  
  18. #ifndef __TYPES__
  19. #include <Types.h>
  20. #endif
  21.  
  22. #ifndef __WINDOWS__
  23. #include <Windows.h>
  24. #endif
  25.  
  26. #ifndef __DESK__
  27. #include <Desk.h>
  28. #endif
  29.  
  30. #ifndef __NOTIFICATION__
  31. #include <Notification.h>
  32. #endif
  33.  
  34. #ifndef __TOOLUTILS__
  35. #include <ToolUtils.h>
  36. #endif
  37.  
  38. #ifndef __MENUS__
  39. #include <Menus.h>
  40. #endif
  41.  
  42.  
  43. #include "const.h"
  44. #include "mymenus.h"
  45. #include "globals.h"
  46. #include "utils.h"
  47. #include "commands.h"
  48. #include "statusdialog.h"
  49. #include "myipm.h"
  50.  
  51. #include "myevents.h"
  52.  
  53. /* main event loop */
  54.  
  55. void MainLoop(void)
  56. {
  57.     EventRecord ev;
  58.     Boolean gotEvent;
  59.  
  60.     while (!gDone) {
  61.         if (gHasWaitNextEvent)
  62.             gotEvent = WaitNextEvent(everyEvent,&ev,SleepTime(),nil);
  63.         else {
  64.             gotEvent = GetNextEvent(everyEvent,&ev);
  65.             SystemTask();
  66.         }
  67.         
  68.         ProcessEvent(&ev);
  69.     }
  70. }
  71.  
  72.  
  73. /* calculate how long to "wait" */
  74.  
  75. long SleepTime(void)
  76. {
  77.     if (gInBackground)
  78.         return kSleepBackground;
  79.     else
  80.         return kSleepForeground;
  81. }
  82.  
  83.  
  84. /* idle time processing */
  85.  
  86. void HandleIdle(void)
  87. {
  88.     MyQElemPtr qBlock;
  89.     
  90.     while (qBlock = GetCompletedQBlock()) {
  91.         ProcessNotification(qBlock);
  92.     }
  93. }
  94.  
  95.  
  96. /* process an event gotten by MainLoop() */
  97.  
  98. void ProcessEvent(EventRecord *ev)
  99. {
  100.     if (HandleDialogEvents(ev))
  101.         return;
  102.  
  103.     switch (ev->what) {
  104.             case mouseDown:
  105.                 HandleMouseDowns(ev);
  106.                 break;
  107.             case keyDown:
  108.             case autoKey:
  109.                 HandleKeyDowns(ev);
  110.                 break;
  111.             case updateEvt:
  112.                 HandleUpdates((WindowPtr)ev->message);
  113.                 break;
  114.             case activateEvt:
  115.                 HandleActivates(ev);
  116.                 break;
  117.             case osEvt:
  118.                 HandleSREvt(ev->message);
  119.                 break;
  120.             case nullEvent:
  121.                 HandleIdle();
  122.                 break;
  123.     }
  124. }
  125.  
  126.  
  127. /* Handles suspend and resume events */
  128.  
  129. void HandleSREvt(long message)
  130. {
  131.     extern NMRec *gNotify;
  132.     extern Boolean gInBackground;
  133.     unsigned long whatMessage;
  134.     
  135.     whatMessage = message >> 24;
  136.     
  137.     if (whatMessage==suspendResumeMessage) {
  138.         if ((message & 1) != 0) {
  139.             gInBackground = false;
  140.             SetCursor(&qd.arrow);
  141.             if (FrontWindow()) {
  142.                 HiliteWindow(FrontWindow(),true);
  143.                 DoActivate(FrontWindow(),true);
  144.             }
  145.         }
  146.         else if (FrontWindow()) {
  147.             gInBackground = true;
  148.             HiliteWindow(FrontWindow(),false);
  149.             DoDeActivate(FrontWindow(),true);
  150.         }
  151.     }
  152.     else if ((whatMessage&mouseMovedMessage)==mouseMovedMessage)
  153.         ;
  154. }
  155.  
  156.  
  157. /* Handles activate and deactivate events for a window */
  158.  
  159. void HandleActivates(EventRecord *ev)
  160. {
  161.     if ((ev->modifiers & activeFlag) != 0) {
  162.         DoActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
  163.     }
  164.     else {
  165.         DoDeActivate((WindowPtr)ev->message,((ev->modifiers & 0x0002) != 0));
  166.     }
  167. }
  168.  
  169.  
  170. /* Handles activate events for a window */
  171.  
  172. void DoActivate(WindowPtr window,Boolean chFlag)
  173. {
  174.     #pragma unused (window,chFlag)
  175. }
  176.  
  177.  
  178. /* Handles deactivate events for a window */
  179.  
  180. void DoDeActivate(WindowPtr window,Boolean chFlag)
  181. {
  182.     #pragma unused (window,chFlag)
  183. }
  184.  
  185.  
  186. /* handles update events for a window */
  187.  
  188. void HandleUpdates(WindowPtr window)
  189. {
  190.     GrafPtr savePort;
  191.     
  192.     GetPort(&savePort);
  193.     SetPort(window);
  194.     BeginUpdate(window);
  195.     EraseRect(&window->portRect);
  196.     EndUpdate(window);
  197.     SetPort(savePort);
  198. }
  199.  
  200.  
  201. /* handles program keydowns */
  202.  
  203. void HandleKeyDowns(EventRecord *ev)
  204. {
  205.     short theChar;
  206.     
  207.     theChar = ev->message & charCodeMask;
  208.     if ((ev->modifiers & cmdKey) != 0)
  209.         DoMenuCommand(MenuKey(theChar));
  210. }
  211.  
  212.  
  213. /* handles mouse down events for a window */
  214.  
  215. void HandleMouseDowns(EventRecord *ev)
  216. {
  217.     WindowPtr window;
  218.     short part;
  219.     Rect limit;
  220.     
  221.     SetRect(&limit,-32000,-32000,32000,32000);
  222.  
  223.     part = FindWindow(ev->where,&window);
  224.     
  225.     switch (FindWindow(ev->where,&window)) {
  226.         case inDrag:
  227.             DragWindow(window,ev->where,&limit);
  228.             break;
  229.         case inGoAway:
  230.             if (TrackGoAway(window,ev->where))
  231.                 gDone = true;
  232.             break;
  233.         case inMenuBar:
  234.             DoMenuCommand(MenuSelect(ev->where));
  235.             break;
  236.         case inSysWindow:
  237.             SystemClick(ev,window);
  238.             break;
  239.     }
  240. }
  241.  
  242.  
  243. /* handles menu commands */
  244.  
  245. void DoMenuCommand(long mResult)
  246. {
  247.     short selItem,selMenu,temp;
  248.     Str255 name;
  249.     GrafPtr tempPort;
  250.     MenuHandle theMenu;
  251.     
  252.     selItem = LoWord(mResult);
  253.     selMenu = HiWord(mResult);
  254.     switch (selMenu) {
  255.         case kAppleMenu:
  256.             if (selItem>2) {
  257.                 GetPort(&tempPort);
  258.                 SetCursor(&qd.arrow);
  259.                 theMenu = GetMHandle(kAppleMenu);
  260.                 GetItem(theMenu,selItem,name);
  261.                 temp = OpenDeskAcc((ConstStr255Param)&name);
  262.                 SetPort(tempPort);
  263.             }
  264.             else CommAbout();
  265.             break;
  266.         case kFileMenu:
  267.             switch (selItem) {
  268.                 case kQuitItem:
  269.                     gDone = true;
  270.                     break;
  271.             }
  272.             break;
  273.         case kEditMenu:
  274.             SystemEdit(selItem-1);
  275.             break;
  276.      }
  277.     HiliteMenu(0);
  278. }
  279.  
  280.  
  281. void ExitProgram(void)
  282. {
  283.     CloseMainDialog();
  284.     CloseIPM();
  285. }
  286.